/**
 * @file SignPage.as

 *  1、修改文案：
 A）原“签名板”标题，改为“嘉宾留影区”及“嘉宾签名区”。
 B）右侧按钮“笔触”改为“字体”；“重签”改为“修改”。
 2、嘉宾留影区：等比例放大，放大后尺寸为643*541，与嘉宾签名区垂直居中对齐。
 * @author db0@qq.com
 * @version 1.0.1
 * @update date 2018-01-15
 */
package futian
{
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	import flash.utils.clearTimeout;

	public class SignPage extends Sprite
	{
		private static var _main:SignPage = null;
		public static function get main():SignPage
		{
			if(_main==null)
				_main = new SignPage();
			return _main;
		}

		private static var bichuBtns:Sprite;
		private var bichu1:BtnBase;
		private var bichu2:BtnBase;
		private var bichu3:BtnBase;
		private function showBichu(e:Event=null):void
		{
			if(bichuBtns==null)
			{
				bichuBtns = new Sprite();
				bichuBtns.x = 1233;
				bichuBtns.y = bichuBtn.y;

				bichu1 = new BtnBase(changeBichu1,new Assets.bichu1_png);
				bichu2 = new BtnBase(changeBichu2,new Assets.bichu2_png);
				bichu3 = new BtnBase(changeBichu3,new Assets.bichu3_png);

				bichu1.load(Assets.bichu1_png_url);
				bichu2.load(Assets.bichu2_png_url);
				bichu3.load(Assets.bichu3_png_url);

				bichuBtns.addChild(bichu1);
				bichuBtns.addChild(bichu2);
				bichuBtns.addChild(bichu3);
				bichu2.x = 150;
				bichu3.x = 300;

			}
			addChild(bichuBtns);
			bichuBtns.visible = true;
		}
		private function changeBichu1():void
		{
			hideBichu();
			drawBoard.lineSize = 5;

		}
		private function changeBichu2():void
		{
			hideBichu();
			drawBoard.lineSize = 10;
		}
		private function changeBichu3():void
		{
			hideBichu();
			drawBoard.lineSize = 15;
		}
		private static function hideBichu():void
		{
			if(bichuBtns)
				bichuBtns.visible = false;
		}

		private static function get titleString():String
		{
			if(Datas.signTitle)
				return Datas.signTitle;
			return "";
			//return "签名板";
		}

		public function SignPage()
		{
			_main = this;
			addChild(new Bitmap(test.main.bg.bitmapData));
			if(Datas.willPlayPictures)
			{
				var title:TextField = ViewSet.makeTxtLine(titleString,33/818*width,0xab3736);
				title.x = (width-title.height)/2;
				title.y = 140;
				addChild(title);
			}else{
				//218,186,400,81
				var title1:TextField = ViewSet.makeTxtLine("嘉宾留影区",70,0xab3736);
				title1.x = 218;
				title1.y = 186;
				addChild(title1);

				//1031,186,403,87
				var title2:TextField = ViewSet.makeTxtLine("嘉宾签名区",70,0xab3736);
				title2.x = 1031;
				title2.y = title1.y;
				addChild(title2);
			}
			rePhotoBtn = new BtnBase(toPhoto,test.newBtn("重拍"));
			photoBtn = new BtnBase(toPhoto,new Assets.takephoto_png);
			if(Datas.willPlayPictures){
				bichuBtn = new BtnBase(showBichu,new Assets.bichu_png);
			}else{
				bichuBtn = new BtnBase(showBichu,new Assets.zit_png);
			}
			saveBtn = new BtnBase(toNext,new Assets.upload_png);
			if(Datas.willPlayPictures)
			{
				resetBtn = new BtnBase(toClear,new Assets.resign_png);
			}else{
				resetBtn = new BtnBase(toClear,new Assets.xiugg1_png);
			}
			backBtn = new BtnBase(quit,new Assets.back_png);

			/*
			   rePhotoBtn.load(Assets.takephoto_png_url);
			   photoBtn.load(Assets.takephoto_png_url);
			   saveBtn.load(Assets.bichu_png_url);
			   resetBtn.load(Assets.resign_png_url);
			   backBtn.load(Assets.back_png_url);
			   bichuBtn.load(Assets.bichu_png_url);
			 */

			if(Datas.willPlayPictures)
				addChild(rePhotoBtn);
			addChild(photoBtn);
			addChild(bichuBtn);
			addChild(resetBtn);

			addChild(saveBtn);

			if(!Datas.willPlayPictures)
				addChild(backBtn);

			saveBtn.x = 
				resetBtn.x = 
				bichuBtn.x = 
				backBtn.x = 
				photoBtn.x = 1700;

			photoBtn.y = 300;
			bichuBtn.y = 400;
			resetBtn.y = 500;
			saveBtn.y = 650;
			backBtn.y = 750;

			rePhotoBtn.x = photoBtn.x + 5;
			rePhotoBtn.y = photoBtn.y + 5;
			//ViewSet.center(rePhotoBtn,photoBtn.x+photoBtn.width/2-1,photoBtn.y+photoBtn.height/2-1,2,2);
			//ViewSet.center(rePhotoBtn,photoBtn.x+photoBtn.width/2-1,photoBtn.y+photoBtn.height/2-1,2,2);

			drawBoard = new DrawBoard();
			drawBoard.x = 816; 
			drawBoard.y = 296; 
			if(Datas.willPlayPictures){
				drawBoard.y = 302; 
			}

			drawBoardBg = new Assets.signBoad_png();
			drawBoardBg.x = drawBoard.x; 
			drawBoardBg.y = drawBoard.y; 
			drawBoardBg.width = drawBoard.width;
			drawBoardBg.height = drawBoard.height;
			addChild(drawBoardBg);
			addChild(drawBoard);

			width = test.stageW;
			height = test.stageH;
		}

		private var rePhotoBtn:BtnBase;
		private var photoBtn:BtnBase;
		private var bichuBtn:BtnBase;
		private var resetBtn:BtnBase;

		private var saveBtn:BtnBase;
		private var backBtn:BtnBase;

		public static var drawBoard:DrawBoard;
		private var drawBoardBg:Bitmap;

		private function toClear():void
		{
			logs.adds("重签");
			hideBichu();
			drawBoard.clear();
		}
		private function toNext():void
		{
			hideBichu();
			if(photoBmp==null)
			{
				Toast.show("请点击“拍照”拍照");
				return;
			}
			if (!drawBoard.hasDrawed)
			{
				Toast.show("请签名");
				return;
			}
			UploadPage.show();
		}

		private static function delBmp(bmp:Bitmap):void
		{
			if(bmp)
			{
				if(bmp.parent)
					bmp.parent.removeChild(bmp);
				bmp.bitmapData.dispose();
			}
		}

		public static function show(clear:Boolean=true):void
		{
			main.visible = true;
			test.main.addChild(main);
			hideBichu();
			if(clear){
				drawBoard.clear();
				if(photoBmp)
				{
					delBmp(photoBmp);
					photoBmp = null;
				}
				if(Datas.willPlayPictures)
				{
					main.rePhotoBtn.visible = false;
				}
				main.photoBtn.visible = true;
			}
			if(MyCam.main.numChildren==0)
			{
				var _x:int = 93;
				var _y:int = drawBoard.y;
				var w:int = 643;
				var h:int = 541;
				if(Datas.willPlayPictures)
				{
					_x = 22;
					_y = 208;
					w = 772;
					h = 642;
				}
				//[Embed(source="btn/paizhaoqu.png")] var paizhaoqu_png :Class;//605x510
				//var _bg:Bitmap = new paizhaoqu_png;
				var _bg:Shape = new Shape();
				with(_bg.graphics)
				{
					clear();
					beginFill(0xffffff,0.1);
					CONFIG::debugging{
						beginFill(0x000000);
					}
					drawRect(0,0,w,h);
					endFill();
				}
				MyCam.main.addChildAt(_bg,0);
				main.addChild(MyCam.main);
				MyCam.main.x = _x;
				MyCam.main.y = _y;
				MyCam.main.setSize(w,h);
			}
		}
		private function quit():void
		{
			NativeApplication.nativeApplication.exit();
		}

		public function toPhoto():void
		{
			rePhotoBtn.visible = false;
			if(Datas.willPlayPictures)
				photoBtn.visible = false;
			if(photoBmp)
			{
				photoBtn.visible = true;
				delBmp(photoBmp);
				photoBmp = null;
				Toast.show("重拍");
				return;
			}
			if(true){
				countDown();
			}else{
				timeCounter = 0;
				showTimeRemain();
			}
		}

		private static var timeoutId:uint;
		private static var timeCounter:int;
		private static var timeTxt:TextField;
		public static var photoBmp:Bitmap;
		private function countDown():void
		{
			if(timeTxt){
				timeTxt.visible = false;
			}
			if(photoBmp)
			{
				if(photoBmp.parent)
					photoBmp.parent.removeChild(photoBmp);
				photoBmp.bitmapData.dispose();
				photoBmp = null;
			}

			timeCounter = Datas.countDown;
			CONFIG::debugging{
				timeCounter = 1;
			}
			showTimeRemain();
		}

		private function showTimeRemain():void
		{
			clearTimeout(timeoutId);
			if(timeTxt==null)
			{
				timeTxt = ViewSet.makeTxtLine("",100,0xffffff);
				timeTxt.alpha=.3;
			}
			timeTxt.text = ""+timeCounter;
			timeTxt.visible = true;
			addChild(timeTxt);
			ViewSet.fullCenter(timeTxt,MyCam.main.x,MyCam.main.y,MyCam.main.width,MyCam.main.height);

			if(timeCounter<=0)
			{
				timeTxt.visible = false;
				photoBmp = new Bitmap(WriteData.displayObject_bitmapDate(MyCam.main));
				addChild(photoBmp);
				photoBmp.x = MyCam.main.x;
				photoBmp.y = MyCam.main.y;
				Toast.show("拍照成功");
				if(Datas.willPlayPictures)
					rePhotoBtn.visible = true;
				else
					photoBtn.visible = true;
			}else{
				timeCounter--;
				timeoutId = setTimeout(showTimeRemain,1000);
			}
		}

	}
}

